package com.xj.zhihudemo.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xj.zhihudemo.R;
import com.xj.zhihudemo.mode.ThemesInfo;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Xujing on 16/9/5.
 */
public class ThemesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    public static final int TYPE_LOAD_MORE = 2;
    private View mHeaderView;
    private Context mContext;
    private LayoutInflater mInflater;
    private List<ThemesInfo.StoriesEntity> mDatas = new ArrayList<ThemesInfo.StoriesEntity>();
    private ArrayList<Integer> idList;
    private ItemClick mItemClick;


    public ThemesAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        idList = new ArrayList<>();

    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    public void addDatas(List<ThemesInfo.StoriesEntity> list) {
        mDatas.addAll(list);
        if (mDatas != null) {
            idList.clear();
            for (ThemesInfo.StoriesEntity storiesEntity : mDatas) {
                idList.add(storiesEntity.getId());
            }
        }
        notifyDataSetChanged();
    }

    public List<ThemesInfo.StoriesEntity> getDatas() {
        return mDatas;

    }

    public ArrayList<Integer> getIdList() {
        return idList;
    }

    public void addHeader(View mHeaderView) {
        this.mHeaderView = mHeaderView;
        notifyDataSetChanged();
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) return new ImageViewHolder(mHeaderView);
        View layout = mInflater.inflate(R.layout.item_news_image, parent, false);
        return new ImageViewHolder(layout);

    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        final int pos = getRealPosition(holder);
        final ThemesInfo.StoriesEntity data = mDatas.get(pos);
        if (holder instanceof ImageViewHolder) {
            if (data.getImages() != null && data.getImages().size() > 0) {
                ((ImageViewHolder) holder).mImageView.setImageURI(
                        mDatas.get(pos).getImages().get(0));
                ((ImageViewHolder) holder).mImageView.setVisibility(View.VISIBLE);
            } else {

                ((ImageViewHolder) holder).mImageView.setVisibility(View.GONE);
            }
            ((ImageViewHolder) holder).mTextView.setText(mDatas.get(pos).getTitle());
            if (mItemClick != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mItemClick.onItemClick(v, pos, data, idList);
                    }
                });
            }
        }

    }

    @Override
    public int getItemCount() {
        return mHeaderView == null ? mDatas.size() : mDatas.size() + 1;
    }

    public void setOnItemClickListener(ItemClick onItemClickListener) {
        mItemClick = onItemClickListener;
    }

    public interface ItemClick {
        void onItemClick(View view, int position, ThemesInfo.StoriesEntity data, ArrayList<Integer> idList);

        void onItemLongClick(View view, int position, ThemesInfo.StoriesEntity data);
    }

    public abstract class ViewHolder extends RecyclerView.ViewHolder {

        protected ViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        protected void update(int position) {
        }

    }

    class ImageViewHolder extends RecyclerView.ViewHolder {

        private TextView mTextView;
        private SimpleDraweeView mImageView;

        public ImageViewHolder(final View itemView) {
            super(itemView);
            if (itemView == mHeaderView) return;
            mTextView = (TextView) itemView.findViewById(R.id.tv_news_text);
            mImageView = (SimpleDraweeView) itemView.findViewById(R.id.iv_news_image);

        }
    }

    public class LoadMoreViewHolder extends ViewHolder {

        protected LoadMoreViewHolder(@NonNull View itemView) {
            super(itemView);
        }

        @Override
        protected void update(int position) {
            super.update(position);
        }
    }
}

